package com.in.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.in.entity.CustomerUserDetails;
import com.in.entity.InUser;
import com.in.mapper.InUserMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserDetailsServiceImpl
 * @Description 鉴权配置服务实现类
 * @Author liulianjia
 * @Date 15:10 2022/7/5
 * @Version 1.0
 **/
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private InUserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        QueryWrapper<InUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", account).eq("status", 1);
        InUser user = userMapper.get(wrapper);
        if (ObjectUtils.isEmpty(user)) {
            throw new UsernameNotFoundException("账号或密码错误！");
        }
        String userId = user.getUserId();

        //用户权限
        List<String> permissions = new ArrayList<>(16);
        permissions.add("/");

        return new CustomerUserDetails(userId, user.getUsername(), user.getPassword(), user.getNickname(),
                user.getAvatar(), user.getGender(), user.getLastLogin(), user.getCreateTime(), permissions);
    }
}

